﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YYS
{
    using config = System.Configuration.ConfigurationManager;
    using System.Diagnostics;

    /// <summary>
    /// Trace
    /// Debug模式：输出；
    /// Realease模式：须在config中配置
    /// AllowTraceWarning：（0|1）
    /// AllowTraceError：（0|1）
    /// AllowTraceInformation：（0|1）
    /// </summary>
    public static class JLog
    {
        public static void TraceWarning(object message)
        {
#if DEBUG
            string msg = message == null ? "object is null" : message.ToString();
            Log(msg, 2);
#endif
#if !DEBUG
            if (config.AppSettings["AllowTraceWarning"] == "1")
            {
                string msg = message == null ? "object is null" : message.ToString();
                Log(msg, 2);
            }
#endif
        }

        public static void TraceWarning(string format, params object[] args)
        {
#if DEBUG
            string msg = string.Format(format, args);
            Log(msg, 2);
#endif
#if !DEBUG
            if (config.AppSettings["AllowTraceWarning"] == "1")
            {
                string msg = string.Format(format, args);
                 Log(msg, 2);
            }
#endif
        }

        static void Log(object message, int Level)
        {
            switch (Level)
            {
                case 1:
                    Trace.TraceInformation(string.Format("**************************************** = {0} = {1}{2}", DateTime.Now, System.Environment.NewLine, message));
                    Trace.Flush();
                    break;
                case 2:
                    Trace.TraceWarning(string.Format("**************************************** = {0} = {1}{2}", DateTime.Now, System.Environment.NewLine, message));
                    Trace.Flush();
                    break;
                case 3:
                    Trace.TraceError(string.Format("**************************************** = {0} = {1}{2}", DateTime.Now, System.Environment.NewLine, message));
                    Trace.Flush();
                    break;
            }
        }

        public static void TraceError(object message)
        {
#if DEBUG
            string msg = message == null ? "object is null" : message.ToString();
            Log(msg, 3);
#endif
#if !DEBUG
            if (config.AppSettings["AllowTraceError"] == "1")
            {
                string msg = message == null ? "object is null" : message.ToString();
                Log(msg, 3);
            }
#endif
        }

        public static void TraceError(string format, params object[] args)
        {
#if DEBUG
            string msg = string.Format(format, args);
            Log(msg, 3);
#endif
#if !DEBUG
            if (config.AppSettings["AllowTraceError"] == "1")
            {
                string msg = string.Format(format, args);
                Log(msg, 3);
            }
#endif
        }

        public static void TraceInformation(object message)
        {
#if DEBUG
            string msg = message == null ? "object is null" : message.ToString();
            Log(msg, 1);
#endif
#if !DEBUG
            if (config.AppSettings["AllowTraceInformation"] == "1")
            {
                string msg = message == null ? "object is null" : message.ToString();
                 Log(msg, 1);
            }
#endif
        }

        public static void TraceInformation(string format, params object[] args)
        {
#if DEBUG
            string msg = string.Format(format, args);
            Log(msg, 1);
#endif
#if !DEBUG
            if (config.AppSettings["AllowTraceInformation"] == "1")
            {
                string msg = string.Format(format, args);
                Log(msg, 1);
            }
#endif
        }
    }
}
